//
//  RequestMethod.m
//  WRY
//
//  Created by Yang on 8/27/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RequestMethod.h"
#import "JSON.h"

@implementation RequestMethod

@synthesize webservicePath,jsonMethodName;
@synthesize webData;
@synthesize notificationName;
@synthesize notificationTarget;
@synthesize targetMethodName;
@synthesize urlPath,urlBody;

//-(void)requestMethod:(NSString *)methodName
//	notificationName:(NSString *)notiname 
//		  pathString:(NSString *)path
//{
//	self.jsonMethodName=methodName;
//	self.notificationName=notiname;
//	[self sendRequestToPath:path];
//}
//-(void)sendRequestToPath:(NSString *)path
//{
//	//webData = [[NSMutableData alloc] init];		
//	self.webservicePath=path;
//	NSString *requesturl=[NSString stringWithFormat:@"%@?%@",self.webservicePath,self.jsonMethodName];
//	NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:requesturl]];
//	[[NSURLConnection alloc]initWithRequest:request delegate:self];
//	//[parametersStr release];
//}
- (void)start {
	[[NSNotificationCenter defaultCenter] addObserver:self.notificationTarget
											 selector:NSSelectorFromString(self.targetMethodName) 
												 name:self.notificationName
											   object:nil];	
	NSString *requesturl=[NSString stringWithFormat:@"%@?MethodName=%@&%@",self.urlPath,self.jsonMethodName,self.urlBody];
	NSLog(@"send : %@",requesturl);
	requesturl = [requesturl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:requesturl]];
	[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
	[[NSURLConnection alloc]initWithRequest:request delegate:self];
	//NSLog(@"send request");
	
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	//NSLog(@"receive response");
	webData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	[webData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
	//NSLog(@"fail errer:",[error description]);
	[connection release];
	[self showErrMsg:@"连接服务器失败！"];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:self.notificationName 
													   object:nil];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {		
	[connection release];
//	NSLog(@"finish receive data");
	//NSLog(@"received data size : %d",[webData length]);
	
	NSString *receivedString = [[NSString alloc] initWithBytes:[webData mutableBytes]
														length:[webData length] encoding:NSUTF8StringEncoding];
//	NSLog(@"%@",receivedString);
	[receivedString release];
	
	NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
	NSError *error;
	SBJSON *json = [[SBJSON new] autorelease];
	NSObject * returnObj = [json objectWithString:responseString error:&error];
	[[NSNotificationCenter defaultCenter]postNotificationName:self.notificationName 
													   object:returnObj];
	[responseString release];
	//[[NSNotificationCenter defaultCenter] removeObserver:self.notificationTarget name:self.notificationName object:nil];
	
}
-(void)showErrMsg:(NSString *)message
{
	UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示信息" 
												 message:message 
												delegate:self 
									   cancelButtonTitle:@"OK" 
									   otherButtonTitles:nil];
	[alert show];
	[alert release];
}
-(void)dealloc
{
	[webservicePath release];
	[jsonMethodName release];
	[webData release];
	[notificationName release];
	[targetMethodName release];
	[urlPath release];
	[urlBody release];
	[super dealloc];
	
}


@end
